/*******************************************************************************
 *
 * File Name:          ListSubjectsAction.java
 *
 * Created:            2006-10-29 8:33:41
 *
 * Original Author:    yu.peng
 *
 *------------------------------------------------------------------------------
 * MODIFICATION HISTORY
 *------------------------------------------------------------------------------
 * WHEN                WHO                 WHY
 * 2006-10-29 8:33:41 yu.peng             new
 *------------------------------------------------------------------------------
 *
 ******************************************************************************/
/*******************/
/* PACKAGE SECTION */
/*******************/
package org.njo.webapp.root.action.forum;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.njo.webapp.root.model.activity.ForumActivity;

/**
 * TODO:comment
 *
 * @author yu.peng
 * @version 0.01
 */
public class ListSubjectAction extends Action {

    // --------------------------------------------------------- Public Methods

    public final static int PAGE_SIZE = 20;

    /**
     * Process the specified HTTP request, and create the corresponding HTTP
     * response (or forward to another web component that will create it).
     * Return an <code>ActionForward</code> instance describing where and how
     * control should be forwarded, or <code>null</code> if the response has
     * already been completed.
     *
     * @param mapping  The ActionMapping used to select this instance
     * @param form     The optional ActionForm bean for this request (if any)
     * @param request  The HTTP request we are processing
     * @param response The HTTP response we are creating
     * @throws Exception if the application business logic throws
     *                   an exception
     */
    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception {

        ForumActivity forum = new ForumActivity();

        String board = request.getParameter("board");
        String pageStr = request.getParameter("page");
        int page = 1;
        try {
            page = Integer.parseInt(pageStr);
        } catch (NumberFormatException e) {
        }
        request.setAttribute("page", String.valueOf(page));

        int startIndex = (page-1) * PAGE_SIZE + 1;
        List subjects = forum.listSubjects(board, startIndex, startIndex + PAGE_SIZE -1);
        request.setAttribute("subjects", subjects);
        Map boardInfo = forum.getBoard(board);
        request.setAttribute("board", boardInfo);

        int size = forum.sizeOfBoard(board);
        request.setAttribute("pageCount", pageCount(size));

        return (new ActionForward("/forum.tiles", false));
    }

    private String pageCount(int size) {
        int cnt = 0;
        if (size % PAGE_SIZE == 0) {
            cnt = size / PAGE_SIZE;
        } else {
            cnt = (size / PAGE_SIZE) + 1;
        }
        return String.valueOf(cnt);
    }



}
